package com.cls.business.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cls.business.entity.Topic;
import com.cls.business.entity.TopicPlan;
import com.cls.business.entity.TopicSelect;
import com.cls.business.service.ITopicPlanService;
import com.cls.business.service.ITopicSelectService;
import com.cls.business.vo.TopicSelectVO;
import com.cls.common.annotation.ControllerEndpoint;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.MyConstant;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.cls.common.enums.DictEnum;
import com.cls.common.exception.MyException;
import com.cls.common.utils.MyUtil;
import com.cls.common.utils.StringUtils;
import com.cls.system.entity.SysDict;
import com.cls.system.service.ISysDictService;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 征集选题 Controller
 *
 * @author wmm
 * @date 2020-11-12 09:22:38
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class TopicSelectController extends BaseController {

    private final ITopicSelectService topicSelectService;

    private final ISysDictService sysDictService;

    private final ITopicPlanService topicPlanService;

    /**
     * 征集选题列表页面
     **/
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/topicSelect")
    public String topicSelectIndex() {
        return MyUtil.view("business/topicSelect/topicSelect");
    }

    /**
     * 课题转换页面
     **/
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/topicSelect/changeTopics/{topicSelectIds}")
    public String topicSelectIndex(@PathVariable String topicSelectIds, Model model) {
        model.addAttribute("topicSelectIds", topicSelectIds);
        //课题类型  年度 委托
        List<SysDict> byCode = sysDictService.findByParentCode(DictEnum.TOPIC_CLASSIFY.getCode());
        model.addAttribute("topicClassifyCode", byCode);
        //查询年度课题分类
        List<SysDict> annualType = sysDictService.findByParentCode(DictEnum.WTKTLB.getCode());
        model.addAttribute("annualType", annualType);

        // 查询不限类型课题分类
        List<SysDict> unlimitedType = sysDictService.findByParentCode(DictEnum.LEVEL_TYPE_UNLIMITED.getCode());
        model.addAttribute("unlimitedType", unlimitedType);
        //查询课题分类
        List<Map<String, Object>> group_list = sysDictService.selectByParentCode(DictEnum.TOPIC_LEVEL_TYPE.getCode());
        Map<String, Object> map = new HashMap<>();
        map.put("groups", group_list);
        model.addAllAttributes(map);
        return MyUtil.view("business/topicSelect/changeTopic");
    }

    /**
     * 征集选题详情页面
     *
     * @return
     */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/topicSelect/topicSelectDetail/{topicSelectId}")
    public String topicSelectDetail(@PathVariable Long topicSelectId, Model model) {
        TopicSelect topicSelect = topicSelectService.getById(topicSelectId);
        model.addAttribute("topicSelect", topicSelect);
        return MyUtil.view("business/topicSelect/topicSelectDetail");
    }

    /**
     * 前端所用接口
     * 列表
     *
     * @param request
     * @param topicSelect
     * @return
     */
    @GetMapping("biz/topicSelect/client/list")
    @ResponseBody
//    @RequiresPermissions("topicSelect:list")
    public ResponseVO topicSelectList2Client(QueryRequest request, TopicSelectVO topicSelect) {
        topicSelect.setCreateBy(getCurrentUser().getUserId());
        PageInfo<TopicSelectVO> pageInfo = this.topicSelectService.findTopicSelectsForClient(request, topicSelect);
        return ResponseVO.success(pageInfo);
    }

    /**
     * 前端所用接口
     * 详情
     *
     * @param
     * @param
     * @return
     */
    @GetMapping("biz/topicSelect/client/detail/{topicSelectId}")
    @ResponseBody
//    @RequiresPermissions("topicSelect:list")
    public ResponseVO topicDetail2Client(@PathVariable Long topicSelectId) {
        TopicSelectVO topicDetail = this.topicSelectService.findTopicDetailForClient(topicSelectId);
        return ResponseVO.success(topicDetail);
    }


    @GetMapping("biz/topicSelect/list")
    @ResponseBody
    @RequiresPermissions("topicSelect:list")
    public ResponseVO topicSelectList(QueryRequest request, TopicSelect topicSelect) {
        PageInfo<TopicSelect> pageInfo = this.topicSelectService.findTopicSelects(request, topicSelect);
        return ResponseVO.success(pageInfo);
    }


    @ControllerEndpoint(operation = "课题转换", exceptionMessage = "课题转换失败")
    @PostMapping("biz/topicSelect/changeTopics/{topicSelectIds}")
    @ResponseBody
    @RequiresPermissions("topicSelect:update")
    public ResponseVO changeTopic(@PathVariable String topicSelectIds, Topic topic) {
        if (StringUtils.isEmpty(topic.getTopicClassifyCode())) {
            throw new MyException("请输入课题分类！");
        }
        String[] ids = topicSelectIds.split(StringPool.COMMA);
        this.topicSelectService.changeTopics(ids, topic);
        return ResponseVO.success();
    }


    @ControllerEndpoint(operation = "新增TopicSelect", exceptionMessage = "新增TopicSelect失败")
    @PostMapping("topicSelect/add")
    @ResponseBody
//    @RequiresPermissions("topicSelect:add")
    public ResponseVO addTopicSelect(@Valid TopicSelect topicSelect) {

        this.topicSelectService.createTopicSelect(topicSelect);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "多个删除TopicSelect", exceptionMessage = "删除TopicSelect失败")
    @PostMapping("topicSelect/deletes/{ids}")
    @ResponseBody
    @RequiresPermissions("topicSelect:delete")
    public ResponseVO deleteTopicSelects(@PathVariable String ids) {
        String[] idArry = ids.split(StringPool.COMMA);
        this.topicSelectService.deleteTopicSelects(idArry);
        return ResponseVO.success();
    }


    @ControllerEndpoint(operation = "删除单个TopicSelect", exceptionMessage = "删除TopicSelect失败")
    @PostMapping("topicSelect/delete/{topicSelectId}")
    @ResponseBody
//    @RequiresPermissions("topicSelect:delete")
    public ResponseVO deleteTopicSelect(@PathVariable Long topicSelectId) {
        this.topicSelectService.removeById(topicSelectId);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改TopicSelect", exceptionMessage = "修改TopicSelect失败")
    @PostMapping("topicSelect/update")
    @ResponseBody
    public ResponseVO updateTopicSelect(TopicSelect topicSelect) {
        Long noticeId = topicSelect.getNoticeId();
        TopicPlan topicPlan = topicPlanService.selectBySelectNoticeId(noticeId);
        topicSelect.setYear(topicPlan.getYear());
        this.topicSelectService.updateTopicSelect(topicSelect);
        return ResponseVO.success();
    }


    @ControllerEndpoint(operation = "提交TopicSelect", exceptionMessage = "提交TopicSelect失败")
    @PostMapping("topicSelect/submit/{selectId}")
    @ResponseBody
    public ResponseVO submitTopicSelect(@PathVariable Long selectId) {
        TopicSelect topicSelect = topicSelectService.getById(selectId);
        this.topicSelectService.submitTopicSelect(topicSelect);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "导出课题征集", exceptionMessage = "导出失败")
    @GetMapping("topicSelect/export")
    public void export(String year, HttpServletResponse response) {
        try {
            this.topicSelectService.export(year, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException("导出失败！");
        }
    }

}
